class Solution(object):
    def isAdditiveNumber(self, num):
        """
        :type num: str
        :rtype: bool
        """

        def check(s1, s2, tail):
            if len(tail)==0: return True
            s3 = str(int(s1)+int(s2))
            if tail.startswith(s3): return check(s2, s3, tail[len(s3):])
            else: return False

        for l1 in range(1,len(num)//2+1):
            s1 = num[0:l1]
            if l1>1 and s1[0]=='0': break
            for l2 in range(1, len(num)-l1*2+1):
                s2 = num[l1:l1+l2]
                if l2>1 and s2[0]=='0': break
                tail = num[l1+l2:]
                if len(tail)<max(l1,l2): break
                if len(tail)>1 and tail[0]=='0': continue
                if check(s1, s2, tail): return True
        return False

print(Solution().isAdditiveNumber('11011'))